<template>
  <div class="box">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">活动管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-alert
        :closable="false"
        title="消息提示的文案"
        type="info"
        center
        show-icon
      >
      </el-alert>

      <el-steps
        :space="200"
        :active="activeName * 1"
        align-center
        finish-status="success"
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>

      <el-form :rules="rules" :model="addgoodsform" ref="addgoodsform">
        <el-tabs
          v-model="activeName"
          tab-position="left"
          @tab-click="tabchange"
          :before-leave="beforeleave"
        >
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="addgoodsform.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input
                type="number"
                v-model="addgoodsform.goods_price"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input
                type="number"
                v-model="addgoodsform.goods_weight"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="addgoodsform.goods_number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
              <p>{{ catecheckarr }}---当前分类id{{ cateId }}</p>
              <div class="block">
                <el-cascader
                  v-model="catecheckarr"
                  :options="catedata"
                  :props="defaultprop"
                  @change="handleChange"
                ></el-cascader>
              </div>
            </el-form-item>
          </el-tab-pane>

          <!-- 动态 menyarr -->
          <el-tab-pane label="商品参数" name="1">
            <el-form-item
              :label="item.attr_name"
              v-for="item in manyarr"
              :key="item.attr_id"
            >
              <el-checkbox-group v-model="item.attr_vals">
                <el-checkbox
                  :label="item1"
                  v-for="(item1, index) in item.attr_vals"
                  :key="index"
                ></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>

          <!-- 静态 -->
          <el-tab-pane label="商品属性" name="2">
            <el-form-item
              :label="item.attr_name"
              v-for="item in onlyarr"
              :key="item.attr_id"
            >
              <el-input v-model="item.attr_vals"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <el-upload
              class="upload-demo"
              action="http://www.ysqorz.top:8888/api/private/v1/upload"
              :on-remove="handleRemove"
              :on-success="handleSuss"
              :headers="headers"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <quill-editor v-model="addgoodsform.goods_introduce" />
            <el-button type="primary" @click="addgood" style="margin-top: 15px"
              >添加商品</el-button
            >
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { goodscatelist, goodsfenl, addgoods } from "@/http/api";
import _ from "lodash";
export default {
  data() {
    return {
      headers: {
        Authorization: sessionStorage.getItem("token"),
      },
      // 商品分配配置项
      defaultprop: {
        expandTrigger: "hover",
        label: "cat_name",
        children: "children",
        value: "cat_id",
      },
      // 已选中分类数组
      catecheckarr: [],
      // 分组中的数据
      catedata: [],
      activeName: "0",
      addgoodsform: {
        // 商品名称
        goods_name: "",
        // 以为‘，’分割的分类列表
        // goods_cat:'',
        // 价格
        goods_price: "",
        // 商品数量
        goods_number: "",
        // 商品重量
        goods_weight: "",
        goods_cat: "",
        goods_introduce: "",
        pics: [],
        attrs: [],
      },
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", triggle: "blur" },
          { min: 3, max: 6, message: "长度在3到5个字符", triggle: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", triggle: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", triggle: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", triggle: "blur" },
        ],
      },
      // 装动态的数组
      manyarr: [],
      // 装静态的数组
      onlyarr: [],
    };
  },
  created() {
    this.getcatelist();
  },

  methods: {
    // 添加
    async addgood() {
      console.log(this.manyarr);
      this.addgoodsform.goods_cat = this.catecheckarr.join(",");
      let manyarrnew = _.clone(this.manyarr);
      manyarrnew = manyarrnew.map((item) => {
        return {
          attr_id: item.attr_id,
          attr_value: item.attr_vals.join(","),
        };
      });
      console.log(manyarrnew);
      let onlyarrnew = _.clone(this.onlyarr);
      onlyarrnew = onlyarrnew.map((item) => {
        return {
          attr_id: item.attr_id,
          attr_value: item.attr_vals,
        };
      });
      this.addgoodsform.attrs = [...manyarrnew, ...onlyarrnew];
      console.log(this.addgoodsform.attrs);
      await addgoods(this.addgoodsform);
      this.$router.push("/goods");
    },
    // 图片删除时触发
    handleRemove(fil) {
      console.log(fil);
      let pathtu = fil.response.data.tmp_path;
      let index = this.addgoodsform.pics.findIndex((item) => {
        return item.pic === pathtu;
      });
      this.addgoodsform.pics.splice(index, 1);
      console.log(this.addgoodsform.pics);
    },
    // 图片上传成功时触发
    handleSuss(res) {
      console.log(res);
      let tu = { pic: res.data.tmp_path };
      this.addgoodsform.pics.push(tu);
    },

    // 点击tab蓝切换
    async tabchange() {
      console.log(this.cateId);
      //  左侧tab栏的动态参数
      if (this.activeName === "1") {
        const res = await goodsfenl(this.cateId);
        // 字符转转为数组
        res.forEach((item) => {
          item.attr_vals = item.attr_vals.split(",");
        });

        this.manyarr = res;
        console.log("动态", this.manyarr);
        //  如果是静态参数
      } else if (this.activeName === "2") {
        const res = await goodsfenl(this.cateId, "only");

        this.onlyarr = res;
        console.log("静态", this.onlyarr);
      }
    },
    // tab栏切换之前进行判断
    beforeleave(newActive, oldActive) {
      if (oldActive === "0" && this.catecheckarr < 3) {
        this.$message({
          message: "您必须选择三级分类后，才能进行下一步",
          type: "error",
        });
        return false;
      }
    },

    // 获取参数
    async getcatelist() {
      const res = await goodscatelist();
      console.log("我", res);
      this.catedata = res;
    },
    handleChange() {
      console.log("分类");
    },
    handleClick() {},
  },
  // 获取数组中最后一个id 来获取参数
  computed: {
    cateId() {
      return this.catecheckarr[this.catecheckarr.length - 1];
    },
  },
};
</script>

<style lang="scss"  scoped>
.box ::v-deep .el-step_title {
  font-size: 12px !important;
}
.el-breadcrumb {
  margin-top: 25px;
}
.el-card {
  margin-top: 25px;
}
.el-steps {
  margin-top: 25px;
}
.el-tabs {
  margin-top: 25px;
}
::v-deep .el-form-item__label {
  display: block;
  width: 100%;
  text-align: left;
}
::v-deep .ql-editor {
  height: 300px;
}
</style>